package com.ev.hr.service.impl;

import com.ev.custom.domain.ContentAssocDO;
import com.ev.custom.service.ContentAssocService;
import com.ev.framework.config.ConstantForDevice;
import com.ev.framework.config.ConstantForHr;
import com.ev.hr.domain.ProfessionalSkillsDO;
import com.ev.hr.vo.ProfessionalSkillsSaveParam;
import com.ev.hr.vo.RewardsPunishmentsSaveParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.ev.hr.dao.RewardsPunishmentsDao;
import com.ev.hr.domain.RewardsPunishmentsDO;
import com.ev.hr.service.RewardsPunishmentsService;
import com.ev.hr.vo.RewardsPunishmentsPageParam;
import com.ev.framework.utils.R;
import com.ev.apis.model.DsResultResponse;
import com.google.common.collect.Maps;


@Service
public class RewardsPunishmentsServiceImpl implements RewardsPunishmentsService {
	@Autowired
	private ContentAssocService contentAssocService;

	@Autowired
	private RewardsPunishmentsDao rewardsPunishmentsDao;
	
	@Override
	public RewardsPunishmentsDO get(Long id){
		return rewardsPunishmentsDao.get(id);
	}
	
	@Override
	public List<RewardsPunishmentsDO> list(Map<String, Object> map){
		return rewardsPunishmentsDao.list(map);
	}
	
	@Override
	public int count(Map<String, Object> map){
		return rewardsPunishmentsDao.count(map);
	}
	
	@Override
	public int save(RewardsPunishmentsDO rewardsPunishments){
		return rewardsPunishmentsDao.save(rewardsPunishments);
	}
	
	@Override
	public int update(RewardsPunishmentsDO rewardsPunishments){
		return rewardsPunishmentsDao.update(rewardsPunishments);
	}

	@Override
	public int updateAll(RewardsPunishmentsDO rewardsPunishments){
		return rewardsPunishmentsDao.updateAll(rewardsPunishments);
	}
	
	@Override
	public int remove(Long id){
		return rewardsPunishmentsDao.remove(id);
	}
	
	@Override
	public int batchRemove(Long[] ids){
		return rewardsPunishmentsDao.batchRemove(ids);
	}

	@Override
	public R saveAndVerify(RewardsPunishmentsSaveParam saveParam){
		/**
		 * 先保存主表信息
		 */
		RewardsPunishmentsDO rewardsPunishmentsDO = saveParam.getRewardsPunishmentsDO();
		if(rewardsPunishmentsDO.getId()!=null){
			rewardsPunishmentsDao.update(rewardsPunishmentsDO);
		}else{
			rewardsPunishmentsDao.save(rewardsPunishmentsDO);
		}
		/**
		 * 先删除附件
		 */
		contentAssocService.removeByAssocIdAndType(new Long[]{rewardsPunishmentsDO.getId()}, ConstantForHr.REWARD_PUNISHMENT_CONTENT);
		/**
		 * 保存附件信息
		 */
		contentAssocService.saveListForEntity(rewardsPunishmentsDO.getId(),saveParam.getContentAssocList(), ConstantForHr.REWARD_PUNISHMENT_CONTENT);
		return R.ok();

	}

	@Override
	public Map<String, Object> getById(Long id) {
		/**
		 * 获取主体信息
		 */
		RewardsPunishmentsDO rewardsPunishmentsDO = rewardsPunishmentsDao.get(id);
		/**
		 * 获取附件数据
		 */
		List<ContentAssocDO> contentAssocList = contentAssocService.list(new HashMap<String,Object>(2){{put("assocId",id);put("assocType",ConstantForHr.REWARD_PUNISHMENT_CONTENT);}});
		/**
		 * 结果组装
		 */
		RewardsPunishmentsSaveParam rewardsPunishmentsDetail = new RewardsPunishmentsSaveParam();
		rewardsPunishmentsDetail.setRewardsPunishmentsDO(rewardsPunishmentsDO);
		rewardsPunishmentsDetail.setContentAssocList(contentAssocList);
		if (rewardsPunishmentsDO != null) {
			Map<String,Object> result = Maps.newHashMap();
			result.put("data",rewardsPunishmentsDetail);
			return R.ok(result);
		}
		return R.error();
	}

	@Override
	public Map<String, Object> pageList(RewardsPunishmentsPageParam pageParam) {
		Map<String,Object> result = Maps.newHashMap();
		List<Map<String, Object>> data = rewardsPunishmentsDao.pageList(pageParam);
		int total = rewardsPunishmentsDao.pageCount(pageParam);
		result.put("data", new DsResultResponse(pageParam.getPageno(),pageParam.getPagesize(),total,data));
		return R.ok(result);
	}

}
